package com.cjm;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName PenetrateServer
 * @Description TODO  穿透软件 ——中间件  实现可供多个客户端程序连接
 * @Author cjm
 * @Date 2020/8/4 13:58
 * @Version 1.0
 **/
public class PenetrateServer {

    public static void main(String[] args) throws IOException {
        int serverPort = 9050;
        int clientNo = 1;

        ServerSocket serverSocket = new ServerSocket(serverPort);

        // 创建线程池
        ExecutorService exec = Executors.newCachedThreadPool();

        try {
            while (true) {
                Socket socket = serverSocket.accept();
                // 每个客户端的请求都由一个独立的线程进行处理
                exec.execute(new SingleServer(socket, clientNo));
                clientNo++;
            }
        } finally {
            serverSocket.close();
        }
    }

}

class SingleServer implements Runnable {

    private Socket socket;
    private int clientNo;

    public SingleServer(Socket socket, int clientNo) {
        this.socket = socket;
        this.clientNo = clientNo;
    }

    @Override
    public void run() {

        try {
            BufferedReader dataReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            DataOutputStream dataOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

            do {

                /*
                * TODO 判断是否带特定服务的指令
                *  如果是 请求转发到特定内网
                */

                // 从客户端接收信息 判断是否为E7服务
                System.out.println("客户端" + clientNo + "ip地址为: " + socket.getInetAddress() + "已连接");
                // 读取的时候 只能读取一遍 第二遍即读取不到
                // 查看请求服务的类型
                String reqServerType = dataReader.readLine();
                System.out.println("请求服务的类型：" + reqServerType);

                // 去掉空格
                if (reqServerType.trim().equalsIgnoreCase("E7")) {
                    // E7客户端
                    System.out.println("********连接到E7客户端**********");

                    Socket socket = new Socket("192.168.15.170", 5052);
                    OutputStream E7Pw = socket.getOutputStream();
                    BufferedReader E7Br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                    String line;
                    if ((line = E7Br.readLine()) != null) {
                        System.out.println(line);
                    }




                    // 发送连接成功的状态码给E7客户端
                    dataOut.writeInt(200);
                    dataOut.flush();
                    // 开始接收E7发送过来的数据
                    String E7Url = dataReader.readLine();
                    System.out.println("E7服务的地址: " + E7Url);

                }else {
                    // 第三方客户端
                    double length = Double.valueOf(dataReader.readLine()).doubleValue();
                    System.out.println("从客户端" + clientNo + "接收到的边长数据为: " + length);
                    double result = length * length;
                    dataOut.writeDouble(result);
                    dataOut.flush();
                }

            } while (dataReader.readLine() != null);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("与客户端" + clientNo + "通信结束");
//            try {
//                socket.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }

    }
}
